<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 * @author   howe <howe1995@163.com>
 */

namespace App\Controller\Api\V1;

use App\Middleware\JWTAuthMiddleware;
use App\Services\MomentService;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\Middlewares;
use Hyperf\HttpServer\Annotation\RequestMapping;

#[Middlewares([JWTAuthMiddleware::class])]
#[Controller(prefix: '/api/v1/moment')]
class MomentController extends CController
{
    #[Inject]
    protected MomentService $momentService;

    /**
     * 朋友圈列表.
     */
    #[RequestMapping(path: 'list', methods: 'get')]
    public function list()
    {
        $params = $this->request->all();
        $params['user'] = $this->user();
        $this->validate($params, [
            'page' => 'integer|min:1',
            'limit' => 'integer|min:1|max:20',
        ]);
        $data = $this->momentService->list($params);
        return $this->setPageInfo($data[0])->success($data[1], $this->request);
    }

    /**
     * 朋友圈详情.
     */
    #[RequestMapping(path: 'detail', methods: 'get')]
    public function detail()
    {
        $params = $this->request->all();
        $params['user'] = $this->user();
        $this->validate($params, [
            'id' => 'required',
        ]);
        $data = $this->momentService->detail($params);
        return $this->success($data, $this->request);
    }

    /**
     * 发朋友圈.
     */
    #[RequestMapping(path: 'publish', methods: ['POST'])]
    public function publish()
    {
        $params = $this->request->all();
        $params['user'] = $this->user();
        $params['files'] = $this->request->file('files');
        $this->validate($params, [
            'type' => 'required',
            'content' => 'required',
        ]);
        $data = $this->momentService->publish($params);
        return $this->success($data, $this->request);
    }

    /**
     * 喜欢朋友圈.
     */
    #[RequestMapping(path: 'like', methods: ['POST'])]
    public function like()
    {
        $params = $this->request->all();
        $params['user'] = $this->user();
        $this->validate($params, [
            'id' => 'required',
        ]);
        $data = $this->momentService->like($params);
        return $this->success($data, $this->request);
    }

    /**
     * 取消喜欢.
     */
    #[RequestMapping(path: 'unlike', methods: ['DELETE'])]
    public function unlike()
    {
        $params = $this->request->all();
        $params['user'] = $this->user();
        $this->validate($params, [
            'id' => 'required',
        ]);
        $data = $this->momentService->unlike($params);
        return $this->success($data, $this->request);
    }

    /**
     * 评论朋友圈.
     */
    #[RequestMapping(path: 'comment', methods: ['POST'])]
    public function comment()
    {
        $params = $this->request->all();
        $params['user'] = $this->user();
        $this->validate($params, [
            'id' => 'required',
            'content' => 'required',
        ]);
        $data = $this->momentService->comment($params);
        return $this->success($data, $this->request);
    }

    /**
     * 朋友圈消息列表.
     */
    #[RequestMapping(path: 'message', methods: ['GET'])]
    public function message()
    {
        $params = $this->request->all();
        $params['user'] = $this->user();
        $data = $this->momentService->message($params);
        return $this->setPageInfo($data[0])->success($data[1], $this->request);
    }

    /**
     * 删除朋友圈.
     */
    public function delete()
    {
        $params = $this->request->all();
        $params['user'] = $this->user();
        $this->validate($params, [
            'id' => 'required',
        ]);
        $data = $this->momentService->delete($params);
        return $this->success($data, $this->request);
    }
}
